const fs = require("fs");
const path = require("path");
const url = require("url");

function getFileMine(extname) {
  let data = fs.readFileSync("./utils/mime.json");
  let mimeObj = JSON.parse(data.toString());
  return mimeObj[extname];
}

exports.static = function (req, res, staticPath) {
  let pathname = url.parse(req.url).pathname;
  pathname = pathname == "/" ? "/index.html" : pathname;
  let extname = path.extname(pathname);
  if (pathname !== "/favicon.ico") {
    try {
      let data = fs.readFileSync("./" + staticPath + pathname);
      if (data) {
        let mime = getFileMine(extname);
        res.writeHead(200, { "Content-Type": "" + mime + ";charset=utf-8" });
        res.end(data);
      }
    } catch (error) {}
  }
};
